package com.winHandle.load;

import com.common.FrameUtil;
import com.common.TimeChange;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * frame 外层框架
 * Created by bao on 2017/8/26.
 */
public class BaseFrame extends JFrame implements ActionListener {

    /* 窗口框架 */
    protected int frameHeight = 550;
    protected int frameWeight = 860;
    protected JFrame jframe ;
    protected JMenuBar jm ;
    protected JMenu table ;
    protected JMenuItem table_1 ;
    protected JMenu field ;
    protected JMenuItem field_1 ;
    protected JMenu project ;
    protected JMenuItem project_1 ;

    protected JMenu about ;
    protected JMenuItem about_1 ;

    protected JLabel time = new JLabel(new SimpleDateFormat("yyyy-MM-dd    hh：mm：ss").format(new Date()));
    protected TimeChange timeChange = new TimeChange(time);

    protected String showFrameName ;

    protected void initFrame(){

        timeChange.start();

        jframe = this;
        jframe.setSize(frameWeight, frameHeight);
        Toolkit kit = Toolkit.getDefaultToolkit();
        jframe.setLocation((kit.getScreenSize().width-frameWeight)/2, (kit.getScreenSize().height-frameHeight)/2);

        jframe.setDefaultCloseOperation(EXIT_ON_CLOSE);

        jm = new JMenuBar();

        table = new JMenu("表单管理");
        table_1 = new JMenuItem("表单记录");

        field = new JMenu("字段管理");
        field_1 = new JMenuItem("字段记录");

        project = new JMenu("项目");
        project_1 = new JMenuItem("项目列表");

        about = new JMenu("关于");
        about_1 = new JMenuItem("退出");

        table_1.addActionListener(this);
        field_1.addActionListener(this);
        about_1.addActionListener(this);
        project_1.addActionListener(this);

        jm.add(table);
        table.add(table_1);

        jm.add(field);
        field.add(field_1);

        jm.add(project);
        project.add(project_1);

        jm.add(about);
        about.add(about_1);

        jframe.setJMenuBar(jm);

        FrameUtil.addIcon(jframe);

        jframe.setJMenuBar(jm);

        JPanel bottom = new JPanel();
        JLabel welcome = new JLabel("欢迎使用数据库笔记工具        ");
        bottom.add(welcome);
        bottom.add(time);
        jframe.getContentPane().add(bottom,BorderLayout.SOUTH);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == table_1){
            if("TABLE".equals(showFrameName)) return;
            if("PROJECT".equals(showFrameName)) jframe.dispose();
            timeChange.stopThread();
            //jframe.dispose();
            jframe.setLocation( jframe.getX()-30, jframe.getY()-30 );
            new MainWin_T("数据库笔记工具");

        }else if(e.getSource() == about_1){

            timeChange.stopThread();
            jframe.dispose();
            new LoadWin("登陆");
        }else if(e.getSource() == project_1){

            timeChange.stopThread();
            jframe.dispose();
            new MainWin_P("数据库笔记工具");

        }else if(e.getSource() == field_1){

            if("FIELD".equals(showFrameName)) return;
            if("PROJECT".equals(showFrameName)) jframe.dispose();
            timeChange.stopThread();
            jframe.setLocation( jframe.getX()-30, jframe.getY()-30 );
            //jframe.dispose();
            new MainWin_F("数据库笔记工具");
        }
    }

    protected BaseFrame(){
        super();
        initFrame();
    }

    protected BaseFrame(String title){
        super(title);
        initFrame();
    }

}
